package com.cloud.bc.service.impl;

import com.cloud.bc.mapper.BcAuthenticationsMapper;
import com.cloud.bc.mapper.BcSinglesMapper;
import com.cloud.bc.model.entity.BcAuthentications;
import com.cloud.bc.model.entity.BcSingleContacts;
import com.cloud.bc.mapper.BcSingleContactsMapper;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.enums.CookieConst;
import com.cloud.bc.service.BcSingleContactsService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.util.CookieUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigInteger;
import java.util.List;

/**
 * 单身联系方式
 *
 * @author douhaichao code generator
 * @date 2021-01-07 11:04:24
 */
@Service
@AllArgsConstructor
public class BcSingleContactsServiceImpl extends ServiceImpl<BcSingleContactsMapper, BcSingleContacts> implements BcSingleContactsService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcSingleContacts selectOne(BcSingleContacts req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcSingleContacts>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcSingleContacts> selectAll(BcSingleContacts req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcSingleContacts> selectPage(Page page, BcSingleContacts req) {
        IPage<BcSingleContacts> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcSingleContacts>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcSingleContacts> selectPageByLike(Page page, BcSingleContacts req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (!StrUtil.isEmpty(req.getWechatid())) {
            queryWrapper.like("wechatId", req.getWechatid());
        }

        if (!StrUtil.isEmpty(req.getWechatQrcode())) {
            queryWrapper.like("wechat_qrcode", req.getWechatQrcode());
        }

        if (!StrUtil.isEmpty(req.getEmail())) {
            queryWrapper.like("email", req.getEmail());
        }

        if (!StrUtil.isEmpty(req.getQq())) {
            queryWrapper.like("qq", req.getQq());
        }

        if (!StrUtil.isEmpty(req.getPhone())) {
            queryWrapper.like("phone", req.getPhone());
        }

        if (null != req.getIsCheck()) {
            queryWrapper.eq("is_check", req.getIsCheck());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (null != req.getMotifyTime()) {
            queryWrapper.eq("motify_time", req.getMotifyTime());
        }
        IPage<BcSingleContacts> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcSingleContacts req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    private final BcAuthenticationsMapper bcAuthenticationsMapper;
    private final BcSinglesMapper bcSinglesMapper;

    @Override
    public String getCenterPop(HttpServletRequest request, HttpServletResponse response, Integer userId) {
        String centerPopCookie = CookieConst.centerPopCookie;
        String cookie = CookieUtils.getCookie(request, centerPopCookie);
        if (!StrUtil.isEmpty(cookie) && cookie.equals("1")) {
            //如果一小时内有弹出过，就不要弹出来了，太烦
            return "";
        }

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        BcSingleContacts bcSingleContacts =
                baseMapper.selectOne(queryWrapper);
        String pop = "";
        //如果联系方式没上传
        if (bcSingleContacts.getIsCheck() == 0) {
            //联系方式未上传
            pop = "contact";
        } else {
            //身份证未上传
            BcAuthentications bcAuthentications =
                    bcAuthenticationsMapper.selectOne(queryWrapper);
            if (bcAuthentications.getIdentityCheck() == 0) {
                pop = "identity";
            } else {
                BcSingles bcSingles = bcSinglesMapper.selectOne(queryWrapper);
                if (bcSingles.getSetmealId() <= 0) {
                    pop = "vip";
                }
            }
        }
        //TODO 上线前改成一小时
        int time = 1000 * 60 * 60;
//        int time = 1000 * 60;
        CookieUtils.setCookie(request, response, centerPopCookie, "1", time);
        return pop;
    }
}
